package com.cys.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cys.bean.AirData;
import com.cys.db.DBHelper;

/**
 * Servlet implementation class AirDataServlet
 */
@WebServlet("/AirDataServlet")
public class AirDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AirDataServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			Connection conn = DBHelper.getConn();
			Statement stmt = conn.createStatement();
			String sql = "select * from t_data_dev_air_monitor order by data_time limit 1";
			ResultSet rs = stmt.executeQuery(sql);
			List<AirData> alist = new ArrayList<AirData>();
			while (rs.next()) {
				AirData airData = new AirData();
				airData.setDev_air_monitor(rs.getInt("dev_air_monitor"));
				airData.setData_time(rs.getString("data_time"));
				airData.setBrightness(rs.getFloat("brightness"));
				airData.setTemperlature(rs.getFloat("temperlature"));
				airData.setWetness(rs.getFloat("wetness"));
				alist.add(airData);
				//System.out.println(alist.size());
			}
			// 将图书集合放置到request之中
			request.setAttribute("alist", alist);
			rs.close(); 
			stmt.close(); 
			conn.close(); 
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("LiveData.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
